<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/12
 * Time: 16:26
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\Aes;
use app\common\lib\Alidayu;
use app\common\lib\IAuth;

class Login extends Common
{
    public function save(){
        $obj = new Aes();
        if(!request()->isPost()){
            return show(config('code.error'),'您没有权限','', 200);
        }
        $param = input('param.');
        if(empty($param['phone'])){
            return show(config('code.error'),'手机号不合法','', 404);
        }
        if(empty($param['code'])){
//            $param['code'] = $obj->decrypt($param['code']);//验证码解密
            return show(config('code.error'),'手机号短信验证码不合法','', 404);
        }
        //validate验证
        $code = Alidayu::getInstance()->checkSmsIdentify($param['phone']);
        if($code != $param['phone']){
            return show(config('code.error'),'手机号验证码不存在','', 404);
        }
        //第一次登录  注册数据
        $token = IAuth::setAppLoginToken($param['phone']);
        $data = [
            'token' => $token,
            'time_out' =>strtotime("+".config('app.login_time_out_day')."days"),
            'username' =>config('app.login_no_username').$param['phone'],
            'status' => 1,
            'phone' => $param['phone']
        ];
        $id = model('User')->add($data);
        if($id){
            $result = [
                $token => $obj->encrypt($token."||".$id),
            ];
            return show(config('code.success'),'OK',$result);
        }
    }
}